package com.ctshk.sapp.custom.controller;


import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.rpc.custom.dto.inquiry.InquiryInfoDTO;
import com.ctshk.rpc.custom.dto.inquiry.InquiryInfoResp;
import com.ctshk.rpc.custom.dto.inquiry.InquiryListDTO;
import com.ctshk.rpc.custom.req.inquiry.*;
import com.ctshk.rpc.custom.service.ICustomizedInquiryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import com.ctshk.common.web.support.BaseController;

import javax.validation.Valid;

/**
 * <p>
 * 询价单 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-13
 */
@Api(value="询价单", tags="询价单管理")
@RestController
@RequestMapping("/inquiry")
public class CustomizedInquiryController extends BaseController {

    @DubboReference
    private ICustomizedInquiryService customizedInquiryService;

    @ApiOperation(value="下单-模拟", notes="询价超时任务demo")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.ADD, operTitle = "发起询价", operDesc = "发起询价单")
    @GetMapping("/test/orders/{id}")
    public Result orders(@PathVariable Long id) {
        customizedInquiryService.expiresTaskDemo(id);
        return Result.success();
    }

    @ApiOperation(value="付款-模拟", notes="取消询价超时任务demo")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.ADD, operTitle = "发起询价", operDesc = "发起询价单")
    @GetMapping("/test/pay/{id}")
    public Result pay(@PathVariable Long id) {
        customizedInquiryService.payTaskDemo(id);
        return Result.success();
    }

    @ApiOperation(value="发起询价", notes="发起询价单")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.ADD, operTitle = "发起询价", operDesc = "发起询价单")
    @PostMapping("")
    public Result add(@RequestBody @Valid InquiryReq inquiryReq) {
        try {
            long id = customizedInquiryService.add(inquiryReq, super.getCurrentUser(getRequest()).getId());
            return Result.success(id);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value="修改询价", notes="修改询价单")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.UPDATE, operTitle = "修改询价", operDesc = "修改询价单")
    @PostMapping("/edit")
    public Result put(@RequestBody @Valid InquiryReq inquiryReq) {
        try {
            return Result.success(customizedInquiryService.edit(inquiryReq,super.getCurrentUser(getRequest()).getId()));
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value="删除询价", notes="发起询价单")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.DELETE, operTitle = "发起询价", operDesc = "发起询价单")
    @DeleteMapping("")
    public Result del() {
        return Result.success();
    }


    @ApiOperation(value="询价单详情", notes="询价单详情(叶冠峰)")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.QUERY, operTitle = "询价单详情", operDesc = "询价单详情")
    @PostMapping("/info")
    public Result<InquiryInfoDTO> inquiryInfo(@RequestBody @Valid InquiryInfoReq inquiryInfoReq) {
        try {
            return customizedInquiryService.query(inquiryInfoReq);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value="询价单列表", notes="询价单列表(叶冠峰)")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.QUERY, operTitle = "询价单列表", operDesc = "询价单列表")
    @PostMapping("/list")
    public Result<PageResponse<InquiryListDTO>> inquiryList(@RequestBody InquiryListReq inquiryListReq) {
        try {
            TokenUser currentUser = getCurrentUser(getRequest());
            PageResponse<InquiryListDTO> list = customizedInquiryService.list(inquiryListReq, currentUser);
            return Result.success(list);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value="接受询价", notes="接受询价(叶冠峰)")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.UPDATE, operTitle = "接受询价", operDesc = "接受询价")
    @PostMapping("/accept")
    public Result acceptInquiry(@RequestBody @Valid InquiryInfoReq inquiryInfoReq) {
        try {
            return customizedInquiryService.acceptInquiry(inquiryInfoReq);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value="拒绝询价", notes="拒绝询价(叶冠峰)")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.UPDATE, operTitle = "拒绝询价", operDesc = "拒绝询价")
    @PostMapping("/refuse")
    public Result refuseInquiry(@RequestBody @Valid InquiryRefuseReq inquiryRefuseReq) {
        try {
            return customizedInquiryService.refuseInquiry(inquiryRefuseReq);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value="下载文件", notes="下载文件(叶冠峰)")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.OTHER, operTitle = "下载文件", operDesc = "下载文件")
    @PostMapping("/down-load-file")
    public void downLoadFile(@RequestBody @Valid InquiryTypeReq inquiryTypeReq) {
        customizedInquiryService.downLoadFile(inquiryTypeReq,getRequest(),getResponse());
    }

    @ApiOperation(value="询价列表操作", notes="询价列表操作(叶冠峰)")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.UPDATE, operTitle = "询价列表操作", operDesc = "询价列表操作")
    @PostMapping("/change-status")
    public Result changeStatus(@RequestBody @Valid InquiryChangeStatusReq inquiryChangeStatusReq) {
        try {
            TokenUser user = getCurrentUser(getRequest());
            return customizedInquiryService.changeStatus(inquiryChangeStatusReq,user.getId());
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

}
